import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_picker_plus/picker.dart';
import 'package:spendbook/common/widgets.dart';
import 'package:spendbook/models/bankCard.dart';
import 'package:spendbook/common/constants.dart';

class AddDepositScreen extends StatefulWidget {
  @override
  _AddDepositScreenState createState() => _AddDepositScreenState();
}

class _AddDepositScreenState extends State<AddDepositScreen> {
  BankCard card = new BankCard();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('添加银行卡'),
        actions: <Widget>[
          TextButton(
            style: Theme.of(context).useMaterial3 ? null : TextButton.styleFrom(foregroundColor: Theme.of(context).colorScheme.onPrimary),
            child: Text(
              '提交',
              style: TextStyle(fontSize: 16),
            ),
            onPressed: () async {
              await card.store();
              Navigator.of(context).pop(card);
            },
          )
        ],
      ),
      body: DepositStatefulWidget(
        card: card,
      ),
    );
  }
}

class SetDepositScreen extends StatefulWidget {
  @override
  _SetDepositScreenState createState() => _SetDepositScreenState();
}

class _SetDepositScreenState extends State<SetDepositScreen> {
  BankCard? card;

  @override
  Widget build(BuildContext context) {
    if (card == null) {
      card = BankCard()..fromJson(ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>);
    }

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('修改银行卡'),
        actions: <Widget>[
          TextButton(
            child: Text(
              '提交',
              style: TextStyle(color: Colors.white, fontSize: 16),
            ),
            onPressed: () async {
              await card?.store();
              Navigator.of(context).pop(card);
            },
          )
        ],
      ),
      body: DepositStatefulWidget(
        card: card!,
      ),
    );
  }
}

class ViewDepositScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    BankCard card = BankCard()..fromJson(ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>);

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('银行卡信息'),
      ),
      body: DepositStatefulWidget(card: card, readonly: true),
    );
  }
}

class DepositStatefulWidget extends StatefulWidget {
  DepositStatefulWidget({Key? key, required this.card, this.readonly = false}) : super(key: key);

  final BankCard card;

  final bool readonly;

  @override
  _DepositStatefulWidgetState createState() => _DepositStatefulWidgetState();
}

class _DepositStatefulWidgetState extends State<DepositStatefulWidget> {
  late BankCard _card;

  @override
  void initState() {
    super.initState();
    _card = widget.card;
  }

  @override
  Widget build(BuildContext context) {
    return PublicList(
      children: <Widget>[
        ListTileScope(
          readonly: widget.readonly,
          title: Row(
            children: <Widget>[
              Expanded(
                child: Text('所属银行'),
              ),
              Text(_card.bank),
            ],
          ),
          trailing: Icon(Icons.chevron_right),
          onTap: () async {
            bool isOther = false;
            await Picker(
                adapter: PickerDataAdapter<String>(
                  pickerData: bankNames,
                ),
                confirmText: '确定',
                cancelText: '取消',
                onConfirm: (Picker picker, List value) {
                  setState(() {
                    _card.bank = bankNames[value[0]];
                  });
                  isOther = value[0] == bankNames.length - 1;
                }).showModal(context);
            if (isOther) {
              final res = await showTextInputDialog(context: context, title: '请输入银行名称', textFields: [
                DialogTextField(),
              ]);
              if (res != null) {
                setState(() {
                  _card.bank = res.first;
                  bankNames.insert(bankNames.length - 1, res.first);
                });
              }
            }
          },
        ),
        ListTileScope(
          readonly: widget.readonly,
          title: Row(
            children: <Widget>[
              Expanded(
                child: Text('金额'),
              ),
              Text(_card.fee.toStringAsFixed(2)),
              Text('元')
            ],
          ),
          trailing: Icon(Icons.chevron_right),
          onTap: () async {
            final res = await showTextInputDialog(context: context, title: '请输入金额', textFields: [
              DialogTextField(
                  initialText: _card.fee > 0 ? _card.fee.toString() : '',
                  keyboardType: TextInputType.numberWithOptions(decimal: true),
                  validator: (val) => val!.isEmpty || double.tryParse(val) != null ? null : '请输入正确的金额'),
            ]);
            if (res != null) {
              setState(() {
                _card.fee = res.first;
              });
            }
          },
        ),
      ],
    );
  }
}
